package com.year_23.may;
/**
 * @author: jsh
 * @create: 2023/5/22 21:52
 * @Description:
 * @FileName: Day22
 * @History:
 * @自定义内容：
 */
public class Day22 {

    public TreeNode sufficientSubset(TreeNode root, int limit) {
        return dfs(root, 0, limit);
    }

    private TreeNode dfs(TreeNode root, int sum, int limit) {
        if (root == null) return null;
        if (root.left == null && root.right ==  null) {
            sum += root.val;
            if (sum < limit) return null;
            return root;
        }
        root.left = dfs(root.left, sum + root.val, limit);
        root.right = dfs(root.right, sum + root.val, limit);

        if (root.left == null && root.right == null)
            return null;
        return root;
    }
}
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}